package jsoupuntil;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.Map;


//Get请求携带cookie信息写法
public class JsoupUntil {
    public static String httpGet(String url,String cookie) throws IOException {
        //获取请求连接
        Connection con = Jsoup.connect(url);
        //请求头设置，特别是cookie设置
        con.header("Accept", "text/html, application/xhtml+xml, */*");
        con.header("Content-Type", "application/x-www-form-urlencoded");
        con.header("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0))");
        con.header("Cookie", cookie);
        //解析请求结果
        Document doc=con.get();
        //获取标题
        System.out.println(doc.title());
        //返回内容
        return doc.toString();

        //带参数写法
//        //获取请求连接
//        Connection conn = Jsoup.connect("http://www.cnblogs.com/zhangfei/p/");
//        //请求参数设置
//        conn.data("page","3");
//        //获取请求结果
//        Document doc = conn.get();

    }



    //Get请求带header和参数的组合写法    还有获取cookie信息写法
    public static String httpGetHeader(String url,String cook,String header) throws IOException{
        //获取请求连接
        Connection con = Jsoup.connect(url);
        //请求头设置，特别是cookie设置
        con.header("Accept", "text/html, application/xhtml+xml, */*");
        con.header("Content-Type", "application/x-www-form-urlencoded");
        con.header("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0))");
        con.header("Cookie", cook);
        //发送请求
        Connection.Response resp=con.method(Connection.Method.GET).execute();
        //获取cookie名称为__bsi的值
        String cookieValue = resp.cookie("__bsi");
        System.out.println("cookie  __bsi值：  "+cookieValue);
        //获取返回cookie所值
        Map<String,String> cookies = resp.cookies();
        System.out.println("所有cookie值：  "+cookies);
        //获取返回头文件值
        String headerValue = resp.header(header);
        System.out.println("头文件"+header+"的值："+headerValue);
        //获取所有头文件值
        Map<String,String> headersOne =resp.headers();
        System.out.println("所有头文件值："+headersOne);
        return headerValue;

    }

    //Post请求带参数和cookies信息

    public static String httpPost(String url,Map<String,String> map,String cookie) throws IOException{
        //获取请求连接
        Connection con = Jsoup.connect(url);
        //遍历生成参数
        if(map!=null){
            for (Map.Entry<String, String> entry : map.entrySet()) {
                //添加参数
                con.data(entry.getKey(), entry.getValue());
            }
        }
        //插入cookie（头文件形式）
        con.header("Cookie", cookie);
        Document doc = con.post();
        System.out.println(doc);
        return doc.toString();



        //post请求获取cookies信息写法


        //发送请求
//        Response resp=con.method(Method.POST).execute();
//        //获取cookie名称为__bsi的值
//        String cookieValue = resp.cookie(header);
//        System.out.println(cookieValue);




        //jsoup发送json数据的方法
//        String url = "http://api.bhao.pro/v2/u/user/register";
//        String referer = "http://www.bhao.pro";
//        Map data = Maps.newHashMap();
//        data.put("countryCode", "+86");
//        data.put("mobile", mobile);
//        data.put("password", "f20332f07cdaab9ec0472655a83438d4");
//        data.put("invitionCode", "PU9HFMNMZ2FL");
//        data.put("validateCode", codeCallable);
//        data.put("geetest_challenge", challenge);
//        data.put("geetest_validate", validate);
//        data.put("geetest_seccode", validate + "|jordan");
//        String json = JSON.toJSONString(data);
//        Connection.Response response = Jsoup.connect(url).ignoreContentType(true).data(data).requestBody(json)
//                .header("Accept", "application/json, text/plain, */*")
//                .header("Content-Type", "application/json;charset=UTF-8")
//                .method(Connection.Method.POST).timeout(70000).execute();
//        System.out.println(response.body());
    }



}
